package hu.dobrosi.dinkycam.j2me.forms;

import hu.dobrosi.dinkycam.j2me.Labels;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.TextField;

public class DecimalTextField extends TextField implements ItemCommandListener {

	private Command defaultValueCommand;
	private String defaultValue;

	public DecimalTextField(String label, String text, int maxSize) {
		super(label, text, maxSize, DECIMAL);
		setItemCommandListener(this);
		defaultValue = text;
		defaultValueCommand = new Command(Labels.DEFAULT_VALUE.toString()
				+ " (" + defaultValue + ")", Command.ITEM, 1);
		addCommand(defaultValueCommand);
	}

	public int getValue() {
		try {
			return Integer.parseInt(getString());
		} catch (Exception e) {
			return -1;
		}
	}

	public void commandAction(Command c, Item i) {
		if (c == defaultValueCommand) {
			setString(defaultValue);
		}
	}
}